package com.wnxy.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String methodName=req.getParameter("method");

        if(methodName==""|| methodName==null){

            methodName="execute";

        }

        Class<? extends BaseServlet> clz = this.getClass();

        try {
            Method method = clz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            method.setAccessible(true);

            String path= (String) method.invoke(this,req,res);

            if(path!=null){
                req.getRequestDispatcher(path).forward(req, res);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }



    }
}
